import 'package:flutter/material.dart';
import 'package:mall/core/providers/pages/mine/user_renew_list_model.dart';
import 'package:mall/core/providers/view_model.dart';
import 'package:provider/provider.dart';

/// 用户续费信息
class UserRenewPage extends StatelessWidget {
  static const routeName = '/UserRenewPage';
  final int args;

  UserRenewPage({
    Key key,
    @required this.args,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => UserRenewListModel(
        Provider.of(context, listen: false),
        args,
      )..businessType = null,
      child: Consumer<UserRenewListModel>(
        builder: (context, model, child) {
          switch (model.viewState) {
            case ViewState.busy:
              return Scaffold(
                appBar: child,
                body: Center(
                  child: CircularProgressIndicator(),
                ),
              );
            case ViewState.error:
              return Scaffold(
                appBar: child,
                body: Center(
                  child: Text(model.errorMessage),
                ),
              );
            case ViewState.success:
              return Scaffold(
                appBar: AppBar(
                  title: Text('续费信息'),
                  actions: <Widget>[
                    FlatButton(
                      onPressed: () {
                        _onTypeClicked(context);
                      },
                      child: Text(
                        '续费类型',
                        style: TextStyle(color: Colors.white),
                      ),
                    )
                  ],
                ),
                body: _BodyWidget(),
              );
            default:
              return Container();
          }
        },
        child: AppBar(
          title: Text('续费信息'),
        ),
      ),
    );
  }

  void _onTypeClicked(BuildContext context) async {
    UserRenewListModel model = Provider.of(context, listen: false);
    var result = await showModalBottomSheet(
      context: context,
      builder: (context) {
        return ChangeNotifierProvider.value(
          value: model,
          child: _TypeBottomSheetWidget(),
        );
      },
    );
    if (result is String) {
      model.businessType = result;
    }
  }
}

class _TypeBottomSheetWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    UserRenewListModel model = Provider.of(context, listen: false);
    final list = model.userRenewList.businessTypeOptions.entries.toList();
    return ListView.builder(
      shrinkWrap: true,
      itemBuilder: (context, index) {
        var mapEntry = list[index];
        return ListTile(
          title: Text(mapEntry.value),
          onTap: () {
            Navigator.of(context).pop(mapEntry.key);
          },
        );
      },
      itemCount: list.length,
    );
  }
}

class _BodyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    UserRenewListModel model = Provider.of(context, listen: false);
    final list = model.userRenewList.renewHistories;
    return ListView.separated(
      itemBuilder: (context, index) {
        var renew = list[index];
        return Material(
          color: Colors.white,
          child: ListTile(
            title: Text(renew.businessName),
            subtitle: Text('${renew.businessLevel}  \n金额：${renew.amount}'),
            trailing: Text(renew.createTime),
            isThreeLine: true,
          ),
        );
      },
      separatorBuilder: (context, index) {
        return Divider(height: 1);
      },
      itemCount: list.length,
    );
  }
}
